//
//  AppDelegate.m
//  嗨文
//
//  Created by 响当当 on 2019/1/7.
//  Copyright © 2019年 dongzheng. All rights reserved.
//

#import "AppDelegate.h"
#import "HWTabBarController.h"
#import "HWLoginViewController.h"
#import "HWCustomerCodingViewController.h"

#import "HWBookViewController.h"
#import "HWMeViewController.h"
#import "HWHomeWebViewController.h"
#import "HWMoneyWebViewController.h"
#import "HWBookCircleViewController.h"
#import "HWApnPushManager.h"


@interface AppDelegate ()<UITabBarControllerDelegate>
@property (nonatomic, strong)HWTabBarController *vc;
@property (nonatomic, strong)HWCustomerCodingViewController *codVC;
@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    
//    self.codVC = [[HWCustomerCodingViewController alloc]init];
//    WeakSelf(self)
//    self.codVC.agreeCodingBlock = ^{
//        weakSelf.vc = [[HWTabBarController alloc] init];
//        weakSelf.vc.delegate = weakSelf;
//        weakSelf.window.rootViewController = weakSelf.vc;
//        [weakSelf.window makeKeyAndVisible];
//
//        [weakSelf resetFMDB];
//        [weakSelf resetSVP];
//        //监控网络状态
//        [HWUtilties monitorNetworking];
//
//    };
//    self.window.rootViewController = self.codVC;
    
    self.vc = [[HWTabBarController alloc] init];
    self.vc.delegate = self;
    self.window.rootViewController = self.vc;
    [self.window makeKeyAndVisible];
    
    
    [APNsManager resetAPNsPushWithLaunchOptions:launchOptions];
    [self resetFMDB];
    [self resetSVP];
    //监控网络状态
    [HWUtilties monitorNetworking];
    [self.window makeKeyAndVisible];
    
    return YES;
}

#pragma 初始化SVP
- (void)resetSVP
{
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
}

-(id<UIViewControllerAnimatedTransitioning>)tabBarController:(UITabBarController *)tabBarController animationControllerForTransitionFromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC
{
    UINavigationController *fromNav = (UINavigationController *)fromVC;
    UINavigationController *toNav = (UINavigationController *)toVC;

    if ([toNav.topViewController isKindOfClass:[HWBookViewController class]])
    {
        HWBookViewController *vc = (HWBookViewController *)toNav.topViewController;
        
        if ([fromNav.topViewController isKindOfClass:[HWHomeWebViewController class]])
        {
            vc.fromId = @"15327e3ab9640909";
            vc.fromUrl = @"/";
        }
        else if ([fromNav.topViewController isKindOfClass:[HWMoneyWebViewController class]])
        {
            vc.fromId = @"5917d93865f2eb91";
            vc.fromUrl = @"/haiEarn";
        }
        else if ([fromNav.topViewController isKindOfClass:[HWBookCircleViewController class]])
        {
            vc.fromId = @"1c2625db947b0327";
            vc.fromUrl = @"/haiMoments";
        }
        else if ([fromNav.topViewController isKindOfClass:[HWMeViewController class]])
        {
            vc.fromId = @"2893ecaa8bb29bd3";
            vc.fromUrl = @"/personal";
        }

    }
    else if([toNav.topViewController isKindOfClass:[HWHomeWebViewController class]])
    {
        HWHomeWebViewController *vc = (HWHomeWebViewController *)toNav.topViewController;
        
        if ([fromNav.topViewController isKindOfClass:[HWBookViewController class]])
        {
            vc.fromId = @"db8c1b12eff98775";
            vc.fromUrl = @"/bookShelf";
        }
        else if ([fromNav.topViewController isKindOfClass:[HWMoneyWebViewController class]])
        {
            vc.fromId = @"5917d93865f2eb91";
            vc.fromUrl = @"/haiEarn";
        }
        else if ([fromNav.topViewController isKindOfClass:[HWBookCircleViewController class]])
        {
            vc.fromId = @"1c2625db947b0327";
            vc.fromUrl = @"/haiMoments";
        }
        else if ([fromNav.topViewController isKindOfClass:[HWMeViewController class]])
        {
            vc.fromId = @"2893ecaa8bb29bd3";
            vc.fromUrl = @"/personal";
        }
    }
    else if([toNav.topViewController isKindOfClass:[HWMoneyWebViewController class]])
    {
        HWMoneyWebViewController *vc = (HWMoneyWebViewController *)toNav.topViewController;
        
        if ([fromNav.topViewController isKindOfClass:[HWBookViewController class]])
        {
            vc.fromId = @"db8c1b12eff98775";
            vc.fromUrl = @"/bookShelf";
        }
        else if ([fromNav.topViewController isKindOfClass:[HWHomeWebViewController class]])
        {
            vc.fromId = @"15327e3ab9640909";
            vc.fromUrl = @"/";
        }
        else if ([fromNav.topViewController isKindOfClass:[HWBookCircleViewController class]])
        {
            vc.fromId = @"1c2625db947b0327";
            vc.fromUrl = @"/haiMoments";
        }
        else if ([fromNav.topViewController isKindOfClass:[HWMeViewController class]])
        {
            vc.fromId = @"2893ecaa8bb29bd3";
            vc.fromUrl = @"/personal";
        }
    }
    else if([toNav.topViewController isKindOfClass:[HWBookCircleViewController class]])
    {
        HWBookCircleViewController *vc = (HWBookCircleViewController *)toNav.topViewController;
        
        if ([fromNav.topViewController isKindOfClass:[HWBookViewController class]])
        {
            vc.fromId = @"db8c1b12eff98775";
            vc.fromUrl = @"/bookShelf";
        }
        else if ([fromNav.topViewController isKindOfClass:[HWHomeWebViewController class]])
        {
            vc.fromId = @"15327e3ab9640909";
            vc.fromUrl = @"/";
        }
        else if ([fromNav.topViewController isKindOfClass:[HWMoneyWebViewController class]])
        {
            vc.fromId = @"5917d93865f2eb91";
            vc.fromUrl = @"/haiEarn";
        }
        else if ([fromNav.topViewController isKindOfClass:[HWMeViewController class]])
        {
            vc.fromId = @"2893ecaa8bb29bd3";
            vc.fromUrl = @"/personal";
        }
    }
    else if([toNav.topViewController isKindOfClass:[HWMeViewController class]])
    {
        HWMeViewController *vc = (HWMeViewController *)toNav.topViewController;
        
        if ([fromNav.topViewController isKindOfClass:[HWBookViewController class]])
        {
            vc.fromId = @"db8c1b12eff98775";
            vc.fromUrl = @"/bookShelf";
        }
        else if ([fromNav.topViewController isKindOfClass:[HWHomeWebViewController class]])
        {
            vc.fromId = @"15327e3ab9640909";
            vc.fromUrl = @"/";
        }
        else if ([fromNav.topViewController isKindOfClass:[HWMoneyWebViewController class]])
        {
            vc.fromId = @"5917d93865f2eb91";
            vc.fromUrl = @"/haiEarn";
        }
        else if ([fromNav.topViewController isKindOfClass:[HWBookCircleViewController class]])
        {
            vc.fromId = @"1c2625db947b0327";
            vc.fromUrl = @"/haiMoments";
        }
    }
    
    return nil;
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    
    if (![current_User.token isValidString])
    {
        HWLoginViewController *vc = [[HWLoginViewController alloc]init];
        vc.hidesBottomBarWhenPushed = YES;
        [[HWUtilties getCurrentVC].navigationController presentViewController:vc animated:YES completion:^{
            
        }];
        return NO;
    }
    else
    {
        NSDictionary *prarmers = @{
                                   @"access_token" : current_User.token                               };
        
        [RegisterAndLoginManager checkTokenParamers:prarmers sucess:^(id  _Nonnull acceptData) {
            NSString *code = [NSString stringWithFormat:@"%@",acceptData];
            if (![code isEqualToString:@"200"])
            {
                HWLoginViewController *vc = [[HWLoginViewController alloc]init];
                vc.hidesBottomBarWhenPushed = YES;
                [[HWUtilties getCurrentVC].navigationController presentViewController:vc animated:YES completion:^{
                }];
            }
        } fail:^(NSError * _Nullable error) {
            current_User.hasLogin = @"0";
        }];
     
        if ([current_User.hasLogin isValidString])
        {
            if ([current_User.hasLogin isEqualToString:@"1"])
            {
                return YES;
            }
            else
            {
                return NO;
            }
        }
        else
        {
            return NO;
        }
    }
    
    return YES;

}


#pragma 初始化fmdb
- (void)resetFMDB
{
    [LCFMDB shareDatabase:@"HW_User"];
}

#pragma 所有登陆页面返回都在首页
- (void)gotoHome
{
    self.vc.selectedViewController = [self.vc.viewControllers objectAtIndex:1];
}

#pragma 所有登陆页面返回都在首页
- (void)gotoBookShelf
{
    self.vc.selectedViewController = [self.vc.viewControllers objectAtIndex:0];
}

//返回赚
- (void)gotoBookEarn
{
    self.vc.selectedViewController = [self.vc.viewControllers objectAtIndex:2];
}

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
    if (![deviceToken isKindOfClass:[NSData class]]) return;
    const unsigned *tokenBytes = (const unsigned *)[deviceToken bytes];
    NSString *hexToken = [NSString stringWithFormat:@"%08x%08x%08x%08x%08x%08x%08x%08x",
                          ntohl(tokenBytes[0]), ntohl(tokenBytes[1]), ntohl(tokenBytes[2]),
                          ntohl(tokenBytes[3]), ntohl(tokenBytes[4]), ntohl(tokenBytes[5]),
                          ntohl(tokenBytes[6]), ntohl(tokenBytes[7])];
    
}
@end
